﻿@{
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    List<WeChatParking.Entity.Enterprise> list = ViewBag.EnterpriseList;
    List<WeChatParking.Entity.Enterprise> Wylist = list.Where(t => t.Enterprise_FCode == "0").ToList();
}
<script type='text/javascript' src='/Res/js/qrcode.min.js' charset='utf-8'></script>
<div class="">
    <div class="ibox-content">
        <div class="jqGrid_wrapper">
            <div class="col-sm-12 jqGrid-toolbar">
                <form class="form-inline search-form" id="search-form">
                    <div class="form-group">
                        <label for="search_order_id">客户端代码</label>
                        <input type="text" class="form-control" name="deviceClient_code">
                    </div>
                    <div class="form-group">
                        <label for="search_user_name">名称</label>
                        <input type="text" class="form-control" name="device_Name">
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <i class="glyphicon glyphicon-search"></i>查询
                    </button>
                </form>
                <div class="btn-group " role="group">
                    <button class="btn btn-primary" id="btn-add">
                        <i class="glyphicon glyphicon-plus"></i>新增
                    </button>
                    <button class="btn btn-success" id="btn-edit">
                        <i class="glyphicon glyphicon-pencil"></i>编辑
                    </button>
                    <button class="btn btn-danger" id="btn-del">
                        <i class="glyphicon glyphicon-trash"></i>删除
                    </button>
                </div>
            </div>
            <table id="grid"></table>
            <div id="grid_pager"></div>
        </div>
    </div>
</div>
<div class="modal fade" id="form-modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">物业信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" method="post" action="/Admin/SaveDevice">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">客户端代码：</label>
                            <div class="col-sm-9">
                                <input type="text" name="DeviceClient_code" id="Device_code" class="form-control" placeholder="请输入客户端代码" required maxlength="50">
                                <input type="hidden" id="ID" name="ID" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">出入口名称：</label>
                            <div class="col-sm-9">
                                <input type="text" id="Enterprise_Name" name="Device_name" class="form-control" placeholder="请输入出入口名称" required maxlength="50">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">物业名称：</label>
                            <div class="col-sm-9">
                                <select class="form-control" name="Enterprise_ID">
                                    @foreach (var item in Wylist)
                                    {
                                        <option value="@item.ID">@item.Enterprise_Name</option>
                                    }

                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">停车场名：</label>
                            <div class="col-sm-9">
                                <select class="form-control" name="TcEnterprise_ID">
                                    @foreach (var item in list)
                                    {
                                        <option value="@item.ID">@item.Enterprise_Name</option>
                                    }

                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">支付加密串：</label>
                            <div class="col-sm-9">
                                <input type="text" name="Api_secret" class="form-control" placeholder="请输入支付加密串" required maxlength="50">
                            </div>
                        </div>

                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="btn-save">保存</button>
            </div>
        </div>
    </div>
</div>

<div id="form-config-dialog" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>
@section BodyFooter{
    <script>
        $(document).ready(function () {

            $("#grid").jqGrid({
                url: '/Admin/DeviceQuery',
                colNames: ["ID", "客户端代码", "名称", "IP", "物业ID", "物业名", "停车场ID", "停车场名", "支付加密串","状态", "创建时间", "下载支付二维码"],
                colModel: [{
                    name: "ID",
                    index: "ID",
                    key: true,
                    width: 30
                },
                {
                    name: "DeviceClient_code",
                    index: "DeviceClient_code",
                    width: 120
                },
                {
                    name: "Device_name",
                    index: "Device_name",
                    width: 120
                },
                {
                    name: "Device_IP",
                    index: "Device_IP",
                    width: 80
                },
                {
                    name: "Enterprise_ID",
                    index: "Enterprise_ID",
                    width: 40
                },
                 {
                     name: "Enterprise_name",
                     index: "Enterprise_name",
                     width: 80
                 },
                  {
                      name: "TcEnterprise_ID",
                      index: "TcEnterprise_ID",
                      width: 40
                  },
                  {
                      name: "TcEnterprise_name",
                      index: "TcEnterprise_name",
                      width: 80
                  }
                  ,
                  {
                      name: "Api_secret",
                      index: "Api_secret",
                      width: 120
                  },
                  {
                      name: "version",
                      index: "version",
                      width: 90,
                      sortable: false,
                      formatter: version
                  }
                  ,
                {
                    name: "create_time",
                    index: "create_time",
                    width: 120,
                    formatter: 'date',
                    formatoptions: { newformat: 'Y-m-d H:i:s' }
                }, {
                    name: "ID",
                    index: "ID",
                    width: 90,
                    sortable: false,
                    formatter: renderQRcode
                }],
                caption: "设备信息",
            });

            $("#grid").adaptGridSizeInWindow();
        });
        $('#search-form').submit(function () {
            var data = $(this).serializeArray();
            var postData = {};
            $.each(data, function (i, n) {
                postData[n.name] = n.value;
            });

            $("#grid").jqGrid('setGridParam', {
                postData: postData, //发送数据
                page: 1
            }).trigger("reloadGrid"); //重新载入


            return false;
        });
        $('#btn-add').click(function () {
            document.getElementById("ID").value = 0;
            document.getElementById("Device_code").disabled = false;
            $('#form-modal').toggleModal(true, ' 新 增 ');
            $('#form-modal form').bindData(null);

        });
        $('#btn-edit').click(function () {
            var row = $("#grid").getGridSelectedData();
            if (!row)
                return;
            document.getElementById("Device_code").disabled = true;
            $('#form-modal form').bindData(row);
            $('#form-modal').toggleModal(true, ' 编 辑 ');
        });

        $.validator.setDefaults({
            submitHandler: function (form) {
                $(form).ajaxSubmit(function (d) {
                    $("#grid").trigger("reloadGrid");
                    $('#form-modal').toggleModal(false);
                    toastr.info('数据保存成功');
                });
            }
        });
        $('#form-modal form').validate()

        $('#btn-save').click(function (form) {
            $('#form-modal form').submit();
        });
        $('#btn-del').click(function () {
            var ids = $("#grid").jqGrid('getGridParam', 'selarrrow');
            if (ids.length == 0)
                return;
            $.confirm('确认删除数据？', function () {
                $.postJsonAjax('/Admin/DeleteDevice', { ids: ids.join(',') }, function (data) {
                    $("#grid").trigger("reloadGrid");
                    if (data.Code == 1) {
                        toastr.info('数据删除成功');
                    } else {
                        toastr.error(data.Message);
                    }

                });
            });
        });
    </script>
    <script>
        function version(cellvalue, options, rowObject)
        {
            var st = rowObject['version'];
            if (st==10) {
                return "在线";
            } else {
                return "掉线";
            }
        }
        function renderQRcode(cellvalue, options, rowObject) {
            var id = rowObject['ID'];
            var Api_secret = rowObject['Api_secret'];
            var name = rowObject['Device_name'];
            return '<button type="button" class="btn btn-primary btn-xs config-icon-item" data-id="' + Api_secret + '" data-entername="' + name + '"><i class="glyphicon glyphicon-download-alt"></i><span> 下载二维码</span></button>';
        }

        $(function () {
            $(document).on('click', '.config-icon-item', function () {
                var url = '/Admin/PayQRcode';
                var Api_secret = $(this).attr('data-id');
                var title = $(this).attr('data-entername');
                var QRcode = "http://cy.xmyzt.com.cn/h5/index?PayApiKey=" + Api_secret;
                $('#form-config-dialog .modal-content').load(url, function () {
                    $('#form-config-dialog').toggleModal(true, '二维码: ' + title);
                    //生成支付的二维码
                    var qrcode = new QRCode("qrcode", {
                        width: 256,
                        height: 256,
                        text: QRcode
                    });
                });
            });
            $('#form-config-dialog').on('click', '#btn-QRcode-save', function () {
                var img = document.getElementById('qrcode').getElementsByTagName('img')[0];
                var canvas = document.createElement('canvas');
                canvas.width = img.width;
                canvas.height = img.height;
                canvas.getContext('2d').drawImage(img, 0, 0);
                url = canvas.toDataURL('image/png');
                var downloadLink = document.getElementById('downloadLink');
                downloadLink.setAttribute('href', url);
                downloadLink.setAttribute('download', '二维码.png');
                downloadLink.click();
                $('#form-config-dialog').toggleModal(false);
            });
        });
    </script>
}